// Handles registration with the external database

string registrationURL = "http://127.0.0.1/sl-www-integration/register-object.php";
string externalURL; // this is what we'll get from SL to get incoming connections
key registrationRequest;    // used to track down the request for registration

default
{
    state_entry()
    {
        llSetText("Registration request...", <0,1,0>, 1);
        llRequestURL();
    }

    http_request(key id, string method, string body)
    {
        if (method == URL_REQUEST_GRANTED)
        {
            llSetText("Connecting to external webserver...", <0,1,0>, 1);
            externalURL = body;
            string myTimestamp = llGetTimestamp();
            registrationRequest = llHTTPRequest(registrationURL, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], 
            "PermURL=" + llEscapeURL(externalURL)
            + "&timestamp=" + myTimestamp
            );
            
            llSetTimerEvent(3600.0);    // if the registration fails, try later

        }
        else if (method == URL_REQUEST_DENIED)
        {
            llSetText("No permanent URL received. Broken.\nWaiting for retry...", <1,0,0>, 1);
            llOwnerSay("Something went wrong, no url. " + body);
        }
        else if (method == "POST" || method == "GET")
        {
            // incoming request for colour change
            //llOwnerSay(body);
            
            if (body == "")
            {
                llHTTPResponse(id, 403, "Empty message received");
            }
            else
            {
                list params = llParseStringKeepNulls(body, ["&", "="], []);
                float colourRed = 0.0;
                float colourGreen = 0.0;
                float colourBlue = 0.0;
               
                //llOwnerSay("List parsed: " + (string) params);
                
                if (llList2String(params, 0) == "delete")
                {
                    if (llList2String(params, 1) == "true")
                    {
                        llDie();
                    }
                }
                
                if (llList2String(params, 0) == "red")
                    colourRed = llList2Float(params, 1);
                if (llList2String(params, 2) == "green")
                    colourGreen = llList2Float(params, 3);
                if (llList2String(params, 4) == "blue")
                    colourBlue = llList2Float(params, 5);
                
                llSay(0, "New colour: red " + (string) colourRed
                    + " green " + (string) colourGreen + " blue " + (string) colourBlue);
                    
                // Change colour:
                
                llSetColor(<colourRed, colourGreen, colourBlue>, ALL_SIDES);

                llHTTPResponse(id, 200, "OK");
            }
        }       
        else
        {
            llHTTPResponse(id, 405, "Method unsupported");
        }
    }
    
    http_response(key request_id, integer status, list metadata, string body)
    {
        if (request_id == registrationRequest)
        {
            if (status == 200)
            {
                llOwnerSay(body);
                llSetText("", <1,1,1>, 1);
            }
            else
            {
                llOwnerSay("Error " +(string)status + ": " + body);
                llSetText("Object NOT registered!", <1,0,0>, 1);
            }
        }
    }
    
    changed(integer c)
    {
        if (c & (CHANGED_REGION | CHANGED_REGION_START | CHANGED_TELEPORT) )
        {
            llRequestURL();
        }
        if (c & CHANGED_INVENTORY)
        {
            llResetScript();
        }
    }
    
    timer()
    {
        llSetTimerEvent(0.0);
        llRequestURL();
    }
}